package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.AlbumInfoService;
import com.atguigu.tingshu.common.config.redssion.RedissonConfig;
import com.atguigu.tingshu.common.login.TsLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.query.album.AlbumInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumInfoVo;
import com.atguigu.tingshu.vo.album.AlbumListVo;
import com.atguigu.tingshu.vo.album.AlbumStatVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.store.PriorityCapableChannelMessageStore;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "专辑管理")
@RestController
@RequestMapping("api/album/albumInfo")
@SuppressWarnings({"all"})
public class AlbumInfoApiController {

	@Autowired
	private AlbumInfoService albumInfoService;

	@Autowired
	private RedissonClient redissonClient;

	@TsLogin
	@Operation(summary = "新增专辑")
	@PostMapping("saveAlbumInfo")
	public Result save(@RequestBody @Validated AlbumInfoVo albumInfoVo) {
		//验证是否登录
		Long userId = AuthContextHolder.getUserId() == null ? 1l : AuthContextHolder.getUserId();
		//	调用服务层保存方法
		albumInfoService.saveAlbumInfo(albumInfoVo, AuthContextHolder.getUserId());
		return Result.ok();
	}
	@TsLogin
    @Operation(summary = "获取当前专辑列表")
	@PostMapping("/findUserAlbumPage/{pageNo}/{pageSize}")
	public Result findUserAlbumPage(@PathVariable("pageNo") Long pageNo,
	                                @PathVariable("pageSize") Long pageSize,
	                                @RequestBody AlbumInfoQuery albumInfoQuery){
		Long userId = AuthContextHolder.getUserId() == null ? 1l : AuthContextHolder.getUserId();
		albumInfoQuery.setUserId(userId);
        //select * from album_info where user_id = 1 limit 10,10;
		Page<AlbumListVo> albumListVoPage = new Page<>(pageNo,pageSize);
		//调用服务层方法
		IPage<AlbumListVo> iPage = albumInfoService.findUserAlbumPage(albumListVoPage,albumInfoQuery);
		return Result.ok(iPage);
	}

	@Operation(summary = "根据id删除专辑")
    @DeleteMapping("/removeAlbumInfo/{albumId}")
	public Result removeAlbumInfo(@PathVariable("albumId")Long albumId ){
         albumInfoService.removeAlbumInfo(albumId);

		return Result.ok();
	}

	@Operation(summary = "根据专辑Id回显数据")
	@GetMapping("/getAlbumInfo/{albumId}")
	public Result getAlbumInfo(@PathVariable("albumId") Long albumId) {
		//  调用服务层方法
		AlbumInfo albumInfo = albumInfoService.getAlbumInfo(albumId);
		//  返回数据
		return Result.ok(albumInfo);
	}

	@Operation(summary = "更新专辑")
	@PutMapping("/updateAlbumInfo/{albumId}")
	public Result updateAlbumInfo(@PathVariable("albumId") Long albumId,
								  @RequestBody AlbumInfoVo albumInfoVo) {
		albumInfoService.updateAlbumInfo(albumId,albumInfoVo);
		return Result.ok();
	}

	@Operation(summary = "查询用户专辑列表")
	@GetMapping("/findUserAllAlbumList")
	public Result findUserAllAlbumList() {
		//哪个用户查询获取用户Id
		Long userId = AuthContextHolder.getUserId() == null ? 1l : AuthContextHolder.getUserId();
		//调用服务层方法: album_info;
		//select * from album_info where user_id = 1;
		List<AlbumInfo> albumInfoList = albumInfoService.findUserAllAlbumList(userId);
		return Result.ok(albumInfoList);
	}

	/**
	 * 通过专辑id获取专辑状态信息
	 */
	@GetMapping("/getAlbumStatVo/{albumId}")
	public Result<AlbumStatVo> getAlbumStatVo(@PathVariable Long albumId){
		AlbumStatVo albumStatVo = albumInfoService.getAlbumStatVo(albumId);
		return Result.ok(albumStatVo);
	}
}

